import gevent
import time

def f1(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        time.sleep(0.5)
        # gevent.sleep(0.5)


def f2(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        time.sleep(0.5)
        # gevent.sleep(0.5)


def f3(n):
    for i in range(n):
        print(gevent.getcurrent(), i)
        time.sleep(0.5)
        # gevent.sleep(0.5)

print("----1----")
g1 = gevent.spawn(f1, 5)
print("----2----")
g2 = gevent.spawn(f2, 5)
print("----3----")
g3 = gevent.spawn(f3, 5)
print("----4----")
g1.join() # gevent碰到延时就切换任务来执行  意思是等待g1执行完毕
g2.join()
g3.join()

# 测试三种情况：1、不加time.sleep和gevent.sleep，2、加time.sleep，3.加gevent.sleep
'''
1和2结果
----1----
----2----
----3----
----4----
<Greenlet "Greenlet-0" at 0x1ac86253a48: f1(5)> 0
<Greenlet "Greenlet-0" at 0x1ac86253a48: f1(5)> 1
<Greenlet "Greenlet-0" at 0x1ac86253a48: f1(5)> 2
<Greenlet "Greenlet-0" at 0x1ac86253a48: f1(5)> 3
<Greenlet "Greenlet-0" at 0x1ac86253a48: f1(5)> 4
<Greenlet "Greenlet-1" at 0x1ac86253d48: f2(5)> 0
<Greenlet "Greenlet-1" at 0x1ac86253d48: f2(5)> 1
<Greenlet "Greenlet-1" at 0x1ac86253d48: f2(5)> 2
<Greenlet "Greenlet-1" at 0x1ac86253d48: f2(5)> 3
<Greenlet "Greenlet-1" at 0x1ac86253d48: f2(5)> 4
<Greenlet "Greenlet-2" at 0x1ac86253e48: f3(5)> 0
<Greenlet "Greenlet-2" at 0x1ac86253e48: f3(5)> 1
<Greenlet "Greenlet-2" at 0x1ac86253e48: f3(5)> 2
<Greenlet "Greenlet-2" at 0x1ac86253e48: f3(5)> 3
<Greenlet "Greenlet-2" at 0x1ac86253e48: f3(5)> 4
'''
#========
''''
----1----
----2----
----3----
----4----
3结果
<Greenlet "Greenlet-0" at 0x1dca2d82a48: f1(5)> 0
<Greenlet "Greenlet-1" at 0x1dca2d82d48: f2(5)> 0
<Greenlet "Greenlet-2" at 0x1dca2d82e48: f3(5)> 0
<Greenlet "Greenlet-0" at 0x1dca2d82a48: f1(5)> 1
<Greenlet "Greenlet-1" at 0x1dca2d82d48: f2(5)> 1
<Greenlet "Greenlet-2" at 0x1dca2d82e48: f3(5)> 1
<Greenlet "Greenlet-0" at 0x1dca2d82a48: f1(5)> 2
<Greenlet "Greenlet-1" at 0x1dca2d82d48: f2(5)> 2
<Greenlet "Greenlet-2" at 0x1dca2d82e48: f3(5)> 2
<Greenlet "Greenlet-0" at 0x1dca2d82a48: f1(5)> 3
<Greenlet "Greenlet-1" at 0x1dca2d82d48: f2(5)> 3
<Greenlet "Greenlet-2" at 0x1dca2d82e48: f3(5)> 3
<Greenlet "Greenlet-0" at 0x1dca2d82a48: f1(5)> 4
<Greenlet "Greenlet-1" at 0x1dca2d82d48: f2(5)> 4
<Greenlet "Greenlet-2" at 0x1dca2d82e48: f3(5)> 4

'''